package.cpath = "./3rd/skynet/luaclib/?.so" 		--经发现所有路径的相对位置都是相对执行脚本的，而不是这个lua文件
package.path = "./3rd/skynet/lualib/?.lua;./GameSrv/sproto/?.lua"

--头文件
local socket = require "client.socket"
local sproto = require "sproto"
local sprotoWg = require "sprotoWg"
local host = sproto.new(sprotoWg.s2c):host "package"
local request = host:attach(sproto.new(sprotoWg.c2s))

--
local fd = assert(socket.connect("127.0.0.1", 1001))

local Message = {}

-----------------------------------------------------------
--通用函数
-----------------------------------------------------------
local function Sleep(n)
   os.execute("sleep " .. n)
end

local function SendPackage(fd, pack)
	local package = string.pack(">s2", pack)
	socket.send(fd, package)
end

local function SendRequest(name, args)
	local str = request(name, args)
	SendPackage(fd, str)
end

--打印数据的二进制
local function PrintBin(s)
    s=string.gsub(s,"(.)",function (x) return string.format("%02X ",string.byte(x)) end)
    return s
end

--根据收到的消息解出名字
local function CallFunction(t, name, args)
    local f = Message[name]
    if f then
        f(args, player)
    end
end

-----------------------------------------------------------
--服务端消息
-----------------------------------------------------------
function Message.LoginOK(args)
	print("ohyeah baby---->Srv Message get [LoginOK]!")
end

-----------------------------------------------------------
--主逻辑 
-----------------------------------------------------------
SendRequest("Login", {token=12345678910})
while true do
	local r = socket.recv(fd)
	if r~=nil then 
		print("------------------------------------------")
		local t, name, args = host:dispatch(r)
		CallFunction(t, name, args)
		print("------------------------------------------")
	else
		socket.usleep(100)
	end
end
